# -*- coding:utf-8 -*-
# @Author: komeiji satori
from sanic import Sanic, request, response

app = Sanic("sanic_service")


@app.post("/test1")
async def test1(request: request.Request):
    select = request.form.get("select")
    where = request.form.get("where")
    return response.text(f"form: {request.form} \nselect: {select!r}, where: {where!r}")


@app.post("/test2")
async def test2(request: request.Request):
    # 会返回一个字节串, 也就是 bytes 对象
    data = request.body
    return response.raw(data)


@app.post("/test3")
async def test3(request: request.Request):
    # 直接通过 request.json 即可获取相应的数据, 会得到一个字典
    json = request.json
    return response.text(f"keys: {tuple(json.keys())}, values: {tuple(json.values())}")


@app.post("/test4")
async def test4(request: request.Request):
    files = request.files
    # 得到的是一个 request.File 对象, 它是一个 namedtuple
    # 只有三个成员: "type", "body", "name", 显然代表的含义不需要我多说
    file = files.get("樱花庄")
    return response.text(
        f"上传的文件名: {file.name}, 文件内容: {str(file.body, 'utf-8')}, 文件类型: {file.type}"
    )


if __name__ == "__main__":
    app.run()
